package middleware

import (
	"github.com/gin-gonic/gin"
	"learngin/service"
	"net/http"
)

// 定义中间
func CheckMemberLogin() gin.HandlerFunc {
	return func(c *gin.Context) {
		tokenString := c.GetHeader("token")
		userId, msg := service.GetTingToken(tokenString)
		if userId > 0 {
			c.Set("userId", userId)
		} else {
			c.JSON(http.StatusUnauthorized, gin.H{"code": userId, "msg": msg})
			c.Abort()
			return
		}
		c.Next()

		//t := time.Now()
		//fmt.Println("中间件执行完毕", userId)
		//t2 := time.Since(t)
		//fmt.Println("time:", t2)
	}
}
